//
//  SongListViewController.swift
//  TimMusic
//
//  Created by nkl on 2024/10/10.
//

import Combine
import Foundation
import TSVideoKit
import UIKit

class SongListViewController<ViewModel: BasicSongListViewModelProtocol>: BasicViewController, UITableViewDelegate {
    lazy var listView: UITableView = {
        let list = UITableView()
        list.delegate = self
        list.dataSource = self.viewModel
        list.register(cell: SongListCell.self)
        list.backgroundColor = .clear
        return list
    }()

    var viewModel: ViewModel

    lazy var emptyView: TimEmptyView = {
        let v = TimEmptyView()
        v.isHidden = true
        v.importButton.addTarget(self, action: #selector(showPhoto), for: .touchUpInside)
        return v
    }()
    

    fileprivate var cancellable: [AnyCancellable] = []
    init(viewModel: ViewModel) {
        self.viewModel = viewModel
        // 调用父类的 init 方法
        super.init(nibName: nil, bundle: nil)
        addNotifaction()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        viewModel.requestData()
    }

    func addNotifaction() {
        NotificationCenter.default.addObserver(self, selector: #selector(reloadDatas), name: kDataChangedNotifactionName, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(reloadDatas), name: .K_ImportSuccessNotifaction, object: nil)
        if let vModel = viewModel as? SongListViewModel {
            vModel.$datas.receive(on: DispatchQueue.main).sink { [weak self] datas in
                guard let self = self else {
                    return
                }
                self.reloadSongData(datas: datas)
            }.store(in: &cancellable)
        }

        if let vModel = viewModel as? SongRecommendListViewModel {
            vModel.$datas.receive(on: DispatchQueue.main).sink { [weak self] datas in
                guard let self = self else {
                    return
                }
                self.reloadSongData(datas: datas)
            }.store(in: &cancellable)
        }
    }
    
    @objc func showPhoto(){
        ImportFilesManager.shared.openPhotoLibrary(parent: self)
    }

    func reloadSongData(datas: [TSVideo]) {
        if datas.isEmpty {
            emptyView.isHidden = false
        } else {
            emptyView.isHidden = true
        }
        listView.reloadData()
    }

    @objc func reloadDatas() {
        viewModel.requestData()
    }

    override func addChildren() {
        super.addChildren()
        view.addSubview(listView)
        view.addSubview(emptyView)
    }

    override func makeConstraints() {
        super.makeConstraints()
        listView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        emptyView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row >= viewModel.datas.count {
            return
        }
        let video = viewModel.datas[indexPath.row]
        PlayDetailManager.shared.playVideo(video: video, list: viewModel.datas, scene: .local)
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44 + 32
    }

    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        .delete
    }

    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
